const {
  request
} = require("../../utils/index.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:false,  //搜索框收缩
    resultList:[],   //互助中心列表数据
    Eliteshare:{
      pageNum:1,   //页数
      pageSize:10,   //条数
      searchLabel:"",    //标签查询  heat最热  collect收藏
      searchName:"",   //名称查询
      articlesLabel:""   //类别查询
    },
    title:[
      {
        title:"商业",
        value:"business"
      },
      {
        title:"生活",
        value:"life"
      }]
  },
  //最热
  heat(e){
    this.setData({
      "Eliteshare.searchLabel":"heat",
    })
    this.init();
  },
  //收藏
  collect(e){
    this.setData({
      "Eliteshare.searchLabel":"collect",
    })
    this.init();
  },
  //搜索框搜索
  result(e){
    this.setData({
      "Eliteshare.searchName":e.detail
    });
    this.init();
  },

  //搜索框收缩状态
  search(){
    this.setData({
      search:true
    })
  },
  //tbs切换
  onTabsItemTap:function(event){
    var index=event.currentTarget.dataset.index.value;
    this.setData({
      currentTabIndex:index,
      "Eliteshare.articlesLabel":index
    })
    this.init();
  },
  result(e){
    this.setData({
      "Eliteshare.searchName":e.detail
    });
    this.init();
  },
  //互助中心列表数据获取
  async init(){
    try {
      let that = this;
      let par = {
        url: "hw/portal/articles/mi/page?token="+wx.getStorageSync('token'),
        method: "GET",
        data:that.data.Eliteshare,
        header:{
          "Content-Type":"application/json"
        }
      }
      let datainfo = await request(par);
      console.log(datainfo)
      if(datainfo.status==100){
        that.setData({
          resultList:datainfo.data
        })
      }
    } catch (error) {
      console.log(error);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onPullDownRefresh: function () {
    this.setData({
      Eliteshare:{
        pageNum:1,   //页数
        pageSize:10,   //条数
        searchLabel:"heat",    //标签查询  heat最热  collect收藏
        searchName:"",   //名称查询
        articlesLabel:"business"   //类别查询
      },
    })
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    wx.showNavigationBarLoading(); //显示加载
    //模拟加载  1秒
    let that = this;
    if(this.data.Eliteshare.pageNum>=this.data.resultList.lastPage){
      wx.showToast({
        title: '滑倒底了',
        icon:"none"
      })
      wx.hideNavigationBarLoading();  //停止加载
      return;
    }
    this.setData({
      "Eliteshare.pageNum":++this.data.Eliteshare.pageNum
    })
    let par = {
      url:"hw/portal/articles/mi/page?token=" + wx.getStorageSync('token'),
      method:"GET",
      data:that.data.Eliteshare,
      header: {
        "Content-Type": "application/json"
      },
    }
    let datainfo = await request(par);
    if(datainfo.data != undefined){
      let lao = that.data.resultList.list //将老数据存入
      if(datainfo.data.list){
        let list = [...lao,...datainfo.data.list]  //新老数据拼接
        that.setData({
          resultList:datainfo.data,  //重新赋值
          "resultList.list": list
        })
      }
    }else{
      // 滑倒底了
    }
    wx.hideNavigationBarLoading(); //停止加载
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    console.log('转发')
    return {
      title: '浩瀚广维',
      path: ''
    }
  }
})